package cn.lanqiao.ui;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTable;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import cn.lanqiao.entity.Users;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class updateUserFrm extends JDialog {

	private static final long serialVersionUID = -4888562677086474066L;
	private JTextField txtAccount;
	private JTextField txtPassword;
	private JTextField txtTelephone;
	private JTextField txtRole;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			updateUserFrm dialog = new updateUserFrm();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public updateUserFrm(Users user,JTable table) {
		this();
		this.txtAccount.setText(user.getAccount());
		this.txtPassword.setText(user.getPassword());
		this.txtTelephone.setText(user.getTelephone());
		this.txtRole.setText(user.getRole());
		this.table = table;
	}

	public updateUserFrm() {
		setTitle("账号修改");
		setBounds(100, 100, 469, 430);
		this.setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		JLabel label = new JLabel("账号：");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label.setBounds(109, 37, 72, 33);
		getContentPane().add(label);
		
		txtAccount = new JTextField();
		txtAccount.setEnabled(false);
		txtAccount.setEditable(false);
		txtAccount.setBounds(206, 40, 112, 33);
		getContentPane().add(txtAccount);
		txtAccount.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("密码：");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		lblNewLabel.setBounds(109, 103, 72, 28);
		getContentPane().add(lblNewLabel);
		
		txtPassword = new JTextField();
		txtPassword.setBounds(206, 103, 112, 28);
		getContentPane().add(txtPassword);
		txtPassword.setColumns(10);
		
		JLabel label_1 = new JLabel("电话号码：");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_1.setBounds(109, 166, 100, 33);
		getContentPane().add(label_1);
		
		txtTelephone = new JTextField();
		txtTelephone.setBounds(223, 170, 112, 31);
		getContentPane().add(txtTelephone);
		txtTelephone.setColumns(10);
		
		JLabel label_2 = new JLabel("身份：");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		label_2.setBounds(109, 231, 72, 28);
		getContentPane().add(label_2);
		
		txtRole = new JTextField();
		txtRole.setEnabled(false);
		txtRole.setEditable(false);
		txtRole.setBounds(206, 232, 86, 33);
		getContentPane().add(txtRole);
		txtRole.setColumns(10);
		
		JButton cancel = new JButton("取消");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//取消
				updateUserFrm.this.dispose();
			}
		});
		cancel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		cancel.setBounds(38, 308, 113, 33);
		getContentPane().add(cancel);
		
		JButton save = new JButton("确认修改");
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//修改
				//取文本框数据
				String account = txtAccount.getText();
				String password = txtPassword.getText();
				String telephone = txtTelephone.getText();
				//封装成一个对象
				Users users = new Users(password,telephone);
				//调业务层
				cn.lanqiao.service.UserService update = new cn.lanqiao.service.impl.UserServiceImpl();
				update.updateUser(account, users);
				//关闭此窗口
				updateUserFrm.this.dispose();
				table.setModel(new DefaultTableModel(update.getAllUsers(),
					new String[] {
						"\u8D26\u53F7", "\u5BC6\u7801", "\u7535\u8BDD\u53F7\u7801", "\u8EAB\u4EFD"
					}
				));
			}
		});
		save.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		save.setBounds(243, 308, 151, 33);
		getContentPane().add(save);
	}

}
